package com.jnu.accounthelper.ui.slideshow;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.adapter.FragmentViewHolder;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.jnu.accounthelper.R;
import com.jnu.accounthelper.databinding.FragmentAccountBinding;
import com.jnu.accounthelper.databinding.FragmentSlideshowBinding;
import com.jnu.accounthelper.db.Account;
import com.jnu.accounthelper.ui.home.HomeFragment;


import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import static androidx.viewpager.widget.PagerAdapter.POSITION_NONE;

public class SlideshowFragment extends Fragment  {

    private SlideshowViewModel slideshowViewModel;
    private FragmentSlideshowBinding binding;
    private RadioGroup radioGroup1 ;
    private RadioGroup radioGroup2 ;
    private String[] titles={"收入", "支出"};
    private String label1="按分类";
    private String label2="本月";
    private MyFragmentAdapter adapter;
    private View root;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        slideshowViewModel =
                new ViewModelProvider(this).get(SlideshowViewModel.class);


        binding = FragmentSlideshowBinding.inflate(inflater, container, false);
        root = binding.getRoot();


        ViewPager2 viewPager2=root.findViewById(R.id.viewpage);
        adapter=new MyFragmentAdapter(this);
        viewPager2.setAdapter(adapter);


        TabLayout tabLayoutHeader=root.findViewById(R.id.tablayout_header);
        TabLayoutMediator tabLayoutMediator=new TabLayoutMediator(tabLayoutHeader, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(titles[position]);
            }
        });
        tabLayoutMediator.attach();
        /*final TextView textView = binding.textSlideshow;
        slideshowViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });*/
        return root;
    }

    public class MyListener implements RadioGroup.OnCheckedChangeListener{

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            int radioButtonId = group.getCheckedRadioButtonId();
            RadioButton rb = (RadioButton)SlideshowFragment.this.root.findViewById(radioButtonId);
            SlideshowFragment.this.label1 = rb.getText().toString();

        }
    }




    private class MyFragmentAdapter extends FragmentStateAdapter {

        public MyFragmentAdapter(@NonNull @NotNull Fragment fragment) {
            super(fragment);
        }


        @NonNull
        @Override
        public Fragment createFragment(int position){

            switch (position)
            {
                case 0:
                    return AccountFragment.newInstance("1",label1,label2);
                default:
                    return AccountFragment.newInstance("2",label1,label2);

            }
        }



        @Override
        public boolean containsItem(long itemId) {
            return super.containsItem(itemId);
        }

        @Override
        public int getItemCount(){
            return 2;
        }


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}